package com.crm.dao;

import com.crm.model.PageBean;
import com.crm.model.role.Student;
import com.crm.util.HibernateUtil;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import java.math.BigInteger;
import java.util.List;

public class StudentDao {

    /**
     * 登陆
     *
     * @param student 学生
     * @return
     */
    public Student login(Student student) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        String hql = "FROM Student AS s WHERE s.cardNo=:cardNo AND s.password=:password";
        Query query = session.createQuery(hql);
        query.setString("cardNo", student.getCardNo());
        query.setString("password", student.getPassword());

        Student resultStudent = (Student) query.uniqueResult();
        session.getTransaction().commit();
        return resultStudent;
    }

    /**
     * 根据id获取学生
     */
    public Student getStudentById(Integer id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Student student = (Student) session.get(Student.class, id);

        session.getTransaction().commit();
        return student;
    }

    /**
     * 保存学生
     */
    public void save(Student student) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        session.merge(student);

        session.getTransaction().commit();
    }

    /**
     * 获取学生集合
     */
    public List<Student> list(Student student, PageBean pageBean) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        StringBuilder hql = new StringBuilder("from Student");
        if (student != null && StringUtils.isNotEmpty(student.getCardNo())) {
            hql.append(" and cardNo like '%").append(student.getCardNo()).append("%'");
        }
        if (student != null && StringUtils.isNotEmpty(student.getName())) {
            hql.append(" and name like '%").append(student.getName()).append("%'");
        }
        Query query = session.createQuery(hql.toString().replaceFirst("and", "where"));
        if (pageBean != null) {
            query.setFirstResult(pageBean.getStart());
            query.setMaxResults(pageBean.getPageSize());
        }
        @SuppressWarnings("unchecked")
        List<Student> studentList = (List<Student>) query.list();

        session.getTransaction().commit();
        return studentList;
    }

    /**
     *  获取符合条件学生的总数
     */
    public int count(Student student){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        StringBuilder sql = new StringBuilder("select count(*) from eo_student");
        if (student != null && StringUtils.isNotEmpty(student.getCardNo())) {
            sql.append(" and cardNo like '%").append(student.getCardNo()).append("%'");
        }
        if (student != null && StringUtils.isNotEmpty(student.getCardNo())) {
            sql.append(" and name like '%").append(student.getName()).append("%'");
        }
        Query query = session.createSQLQuery(sql.toString().replaceFirst("and", "where"));
        int count = ((BigInteger) query.uniqueResult()).intValue();

        session.getTransaction().commit();
        return count;
    }

    /**
     *  删除学生
     */
    public void delete(Student student) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        session.delete(student);

        session.getTransaction().commit();
    }
}
